package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Exam;
import com.ruoyi.system.domain.Paper;
import com.ruoyi.system.domain.PaperSubject;
import com.ruoyi.system.domain.po.QwPo;
import com.ruoyi.system.domain.vo.StudentSubjectVo;
import com.ruoyi.system.mapper.ExamMapper;
import com.ruoyi.system.mapper.PaperMapper;
import com.ruoyi.system.mapper.PaperSubjectMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.StudentSubject;
import com.ruoyi.system.service.IStudentSubjectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学生考题表Controller
 *
 * @author ruoyi
 * @date 2022-11-20
 */
@RestController
@RequestMapping("/system/subject/student")
public class StudentSubjectController extends BaseController
{
    @Autowired
    private IStudentSubjectService studentSubjectService;

    @Autowired
    private ExamMapper examMapper;

    @Autowired
    private PaperMapper paperMapper;

    @Autowired
    private PaperSubjectMapper paperSubjectMapper;

    @GetMapping("/select/count/{studentId}/{examId}")
    public AjaxResult selectCount(@PathVariable Long studentId,@PathVariable Long examId){
        Exam exam = examMapper.selectExamByExamId(examId);
        Date endTime = exam.getEndTime();
        Long paperId = exam.getPaperId();
        Paper paper = paperMapper.selectPaperByPaperId(paperId);
        String paperName = paper.getName();
        PaperSubject paperSubject=new PaperSubject();
        paperSubject.setPaperId(paperId);
        List<PaperSubject> paperList = paperSubjectMapper.selectPaperSubjectList(paperSubject);
        List<Long> listSubject=new ArrayList<>();
        for (int i = 0; i < paperList.size(); i++) {
            Long id = paperList.get(i).getSubjectId();
            listSubject.add(id);
        }
        StudentSubject studentSubject=new StudentSubject();
        studentSubject.setStudentId(studentId);
        studentSubject.setExamId(examId);
        List<StudentSubject> lists = studentSubjectService.selectStudentSubjectList(studentSubject);
        Integer wanchengtimushuliang=lists.size();
        Integer timuzongshuliang=listSubject.size();
        QwPo qwPo=new QwPo();
        qwPo.setPaperName(paperName);
        qwPo.setTimuzongshuliang(timuzongshuliang);
        qwPo.setEndTime(endTime);
        qwPo.setWanchengtimushuliang(wanchengtimushuliang);
        return success(qwPo);
    }

    /**
     * 查询学生考题表列表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:list')")
    @GetMapping("/list")
    public TableDataInfo list(StudentSubject studentSubject)
    {
        startPage();
        List<StudentSubject> list = studentSubjectService.selectStudentSubjectList(studentSubject);
        return getDataTable(list);
    }

    /**
     * 导出学生考题表列表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:export')")
    @Log(title = "学生考题表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StudentSubject studentSubject)
    {
        List<StudentSubject> list = studentSubjectService.selectStudentSubjectList(studentSubject);
        ExcelUtil<StudentSubject> util = new ExcelUtil<StudentSubject>(StudentSubject.class);
        util.exportExcel(response, list, "学生考题表数据");
    }

    /**
     * 获取学生考题表详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:subject:query')")
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable("studentId") Long studentId)
    {
        return success(studentSubjectService.selectStudentSubjectByStudentId(studentId));
    }

    /**
     * 新增学生考题表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:add')")
    @Log(title = "学生考题表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody StudentSubjectVo studentSubject)
    {
        int subject = studentSubjectService.insertStudentSubject(studentSubject);
        if(subject==-12){
            return error("超时");
        }
        return toAjax(subject);
    }

    /**
     * 修改学生考题表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:edit')")
    @Log(title = "学生考题表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody StudentSubject studentSubject)
    {
        return toAjax(studentSubjectService.updateStudentSubject(studentSubject));
    }

    /**
     * 删除学生考题表
     */
    @PreAuthorize("@ss.hasPermi('system:subject:remove')")
    @Log(title = "学生考题表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studentIds}")
    public AjaxResult remove(@PathVariable Long[] studentIds)
    {
        return toAjax(studentSubjectService.deleteStudentSubjectByStudentIds(studentIds));
    }
}
